package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;

    public PageInfo<Travel> listByAuthorId(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectAll(qo);
        return new PageInfo<>(list);
    }

    public void save(Travel travel, String content) {
        travel.setCreateTime(new Date());
        travel.setLastUpdateTime(new Date());
        travelMapper.insert(travel);
        TravelContent travelContent = new TravelContent();
        travelContent.setId(travel.getId());
        travelContent.setContent(content);
        contentMapper.insert(travelContent);
    }

    public void update(Travel travel, String content) {
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);
        contentMapper.updateByPrimaryKey(travel.getTravelContent());
    }
    public PageInfo<Travel> queryForList(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public TravelContent getContent(Long id) {
        TravelContent content = contentMapper.selectByPrimaryKey(id);
        if (content == null) {
            throw new RuntimeException("该游记没有内容");
        }
        return content;
    }

    public void updateState(Long id, Integer state) {
        Travel travel = new Travel();
        if (state == Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
        }
        travel.setId(id);
        travel.setState(state);
        travelMapper.updateState(travel);
    }

    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    public PageInfo<?> selectTravelByStrategyId(StrategyCatalogQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.selectTravelByStrategyId(qo);
        return new PageInfo<>(list);
    }

    public void deleteTravelId(Long travelId) {
        travelMapper.deleteTravelId(travelId);
    }

    public void saveStrategyAndTravel(Long strategyId, Long travelId) {
        travelMapper.saveStrategyAndTravel(strategyId,travelId);
    }

    public List<Travel> selectAllReaseTravel() {
        return travelMapper.selectAllReaseTravel();
    }

    public Long getTravelsNum(QueryObject qo) {
        return travelMapper.getTravelsNum(qo);
    }

    public int getAmount(Long userId) {
        return travelMapper.getAmount(userId);
    }

    public List<Travel> selectAll(String keyWord) {
        return travelMapper.selectAllByKeyWord(keyWord);
    }
}
